<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单例模式之弹出层</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
        .mask{
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, .3);
            position: absolute;
            top: 0;bottom: 0;left: 0;right: 0;
            margin: auto;
            text-align: center;
            line-height: 500px;
        }
    </style>
</head>
<body>
    <button class="btn">弹出遮罩层</button>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script>
        var Mask = (function(){
            var instance = null;
            return function(elem){
                if(instance){
                    instance.show(elem)
                    return instance
                }
                instance = this;
                // 创建
                instance.createElement = function(){
                    instance.ele = $('<div class = mask>');
                    instance.ele.html(elem);
                    $('body').append(instance.ele);
                    // 绑定事件
                    instance.ele.on('click',function(){
                        instance.ele.hide();
                    })
                }

                // 显示
                instance.show = function(str){
                    // 传入新内容时，修改内容
                    instance.ele.html(str || elem)
                    instance.ele.show()
                }

                instance.createElement();
            }


        })();

        new Mask('<h1>弹出层</h1>');

        $('.btn').on('click',function(){
            new Mask("这是一个更改之后的内容");
        })
    </script>
</body>
</html>